package com.movie.controller;

import com.movie.dto.ActorDTO;
import com.movie.dto.ApiResponse;
import com.movie.dto.PageResponse;
import com.movie.service.ActorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/actors")
@RequiredArgsConstructor
@Tag(name = "演员管理", description = "演员信息管理接口")
public class ActorController {
    
    private final ActorService actorService;
    
    @GetMapping
    @Operation(summary = "获取演员列表", description = "分页获取演员列表，支持关键词搜索")
    public ResponseEntity<ApiResponse<PageResponse<ActorDTO>>> getActors(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String keyword) {
        PageResponse<ActorDTO> actors = actorService.getAllActors(page, size, keyword);
        return ResponseEntity.ok(ApiResponse.success(actors));
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取演员详情", description = "根据ID获取演员详细信息")
    public ResponseEntity<ApiResponse<ActorDTO>> getActor(@PathVariable Long id) {
        ActorDTO actor = actorService.getActorById(id);
        return ResponseEntity.ok(ApiResponse.success(actor));
    }
    
    @PostMapping
    @Operation(summary = "创建演员", description = "创建新的演员信息")
    public ResponseEntity<ApiResponse<ActorDTO>> createActor(@RequestBody ActorDTO actorDTO) {
        ActorDTO createdActor = actorService.createActor(actorDTO);
        return ResponseEntity.ok(ApiResponse.success(createdActor));
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新演员", description = "更新演员信息")
    public ResponseEntity<ApiResponse<ActorDTO>> updateActor(
            @PathVariable Long id, 
            @RequestBody ActorDTO actorDTO) {
        ActorDTO updatedActor = actorService.updateActor(id, actorDTO);
        return ResponseEntity.ok(ApiResponse.success(updatedActor));
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除演员", description = "删除演员信息")
    public ResponseEntity<ApiResponse<Void>> deleteActor(@PathVariable Long id) {
        actorService.deleteActor(id);
        return ResponseEntity.ok(ApiResponse.success(null));
    }
    
    @GetMapping("/search")
    @Operation(summary = "搜索演员", description = "根据关键词搜索演员")
    public ResponseEntity<ApiResponse<List<ActorDTO>>> searchActors(
            @RequestParam String keyword) {
        List<ActorDTO> actors = actorService.searchActors(keyword);
        return ResponseEntity.ok(ApiResponse.success(actors));
    }
}